package com.xy.work.template.base.config;
import cn.hutool.core.util.StrUtil;
import com.xy.work.template.base.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @author niuniu
 * @Date: 2021/5/14 06:13
 * @Description: 这里完成拦截器相关配置生效注册功能
 */
@Slf4j
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    /**
     * 得到项目名称
     */
    @Value("${spring.application.name}")
    private String applicationName;
    /**
     * 添加拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截所有请求，通过判断是否有 @LoginRequired 注解 决定是否需要登录
        //注册登陆拦截器
        InterceptorRegistration loginInterceptor = registry.addInterceptor(loginInterceptor());
        String loginInterceptorUrl=null;
        if(StrUtil.isEmpty(applicationName)){
            loginInterceptorUrl="/**";
        }else {
            loginInterceptorUrl="/"+applicationName+"/**";
        }
        log.info("注册配置登陆拦截器网址：{}",loginInterceptorUrl);
        //拦截所有
        loginInterceptor.addPathPatterns(loginInterceptorUrl);
        //排除接口文档
        loginInterceptor.excludePathPatterns("/swagger-ui/index.html");
    }

    /**
     * 返回登录拦截器
     * @return
     */
    @Bean
    public LoginInterceptor loginInterceptor() {
        log.info("创建登陆拦截器");
        return new LoginInterceptor();
    }
//    /**
//     *  设置资源访问路径
//     * @param registry
//     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:/META-INF/resources/")
//                .addResourceLocations("classpath:/resources/")
//                .addResourceLocations("classpath:/static/")
//                .addResourceLocations("classpath:/public/");
//    }

}
